package com.banmao.algorithm.utils;

import com.banmao.algorithm.utils.vo.TreeNode;
import java.util.LinkedList;
import java.util.Queue;

public class GenerateTreeUtil {

    public static TreeNode generateTree(String[] array) {

        LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
        TreeNode root = new TreeNode(Integer.parseInt(array[0]));
        queue.add(root);

        // 题目中一般提供的示例是层序遍历
        int index = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.pollFirst();
                if(index >= array.length) {
                    break;
                }
                if (poll != null) {
                    String leftValue = array[index++];
                    String rightValue = array[index++];
                    poll.left = "null".equalsIgnoreCase(leftValue) ? null : new TreeNode(Integer.parseInt(leftValue));
                    queue.add(poll.left);
                    poll.right = "null".equalsIgnoreCase(rightValue) ? null : new TreeNode(Integer.parseInt(rightValue));
                    queue.add(poll.right);
                }
            }
        }

        return root;

    }


    static final String SEP = ",";
    static final String NULL = "#";

    public static TreeNode generateTree2(String data) {
        if (data.isEmpty()) return null;
        String[] nodes = data.split(SEP);
        // 第一个元素就是 root 的值
        TreeNode root = new TreeNode(Integer.parseInt(nodes[0]));

        // 队列 q 记录父节点，将 root 加入队列
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);

        for (int i = 1; i < nodes.length; ) {
            // 队列中存的都是父节点
            TreeNode parent = q.poll();
            // 父节点对应的左侧子节点的值
            String left = nodes[i++];
            if (!left.equals(NULL)) {
                parent.left = new TreeNode(Integer.parseInt(left));
                q.offer(parent.left);
            } else {
                parent.left = null;
            }

            if (i >= nodes.length) {
                continue;
            }

            // 父节点对应的右侧子节点的值
            String right = nodes[i++];
            if (!right.equals(NULL)) {
                parent.right = new TreeNode(Integer.parseInt(right));
                q.offer(parent.right);
            } else {
                parent.right = null;
            }
        }
        return root;
    }

    /* 将二叉树序列化为字符串 */
    public static String serialize(TreeNode root) {
        if (root == null) return "";
        StringBuilder sb = new StringBuilder();
        // 初始化队列，将 root 加入队列
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);

        while (!q.isEmpty()) {
            TreeNode cur = q.poll();

            /* 层级遍历代码位置 */
            if (cur == null) {
                sb.append(NULL).append(SEP);
                continue;
            }
            sb.append(cur.val).append(SEP);
            /*****************/

            q.offer(cur.left);
            q.offer(cur.right);
        }

        return sb.toString();
    }

}


